﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Collections;

namespace DataAccess
{
    public class CookDAO
    {
        public Object doOneFood(string cookID)
        {
            SqlConnection con = null;
            try
            {
                con = ConnectionManager.getInstance().getConnection();

                // create a sql command object
                string strSql = string.Format("select tb_orderitem.id, tb_orderitem.foodID, foodName, tb_orderitem.notes "
                        + "from tb_menu,tb_orderitem,tb_orderlist "
                        + "where itemState={1} and tb_orderitem.foodID = tb_menu.foodID "
                        + "and tb_orderitem.orderID = tb_orderlist.orderID "
                        + "and cookID in ('', '{1}') "
                        +"and tb_orderlist.orderState={0} "
                        + " order by orderTime",
                        OrderDAO.ORDER_STATE_ORDERED,
                        OrderDAO.ITEM_STATE_WAITING,
                        cookID);
                SqlCommand command = new SqlCommand(strSql, con);

                // call ExecuteReader() to get a data reader
                SqlDataReader sdr = command.ExecuteReader(CommandBehavior.CloseConnection);

                Hashtable showinfo = new Hashtable();
                // circle all record
                if (sdr.Read())
                {
                    showinfo.Add("id", sdr["id"].ToString().Trim());
                    showinfo.Add("foodID", sdr["foodID"].ToString().Trim());
                    showinfo.Add("foodName", sdr["foodName"].ToString().Trim());
                    showinfo.Add("notes", sdr["notes"].ToString().Trim());
                }

                sdr.Close();
                return showinfo;
            }
            catch (Exception e) { throw e; }
            finally
            {
                if (con != null)
                {
                    con.Close();
                    con.Dispose();
                    con = null;
                }
            }
        }

        public bool setComplete(string strCookID, string strID)
        {
            SqlConnection con = null;
            try
            {
                con = ConnectionManager.getInstance().getConnection();

                string strSql = "update tb_orderitem set itemState=1 where id=" + strID + ";";
                SqlCommand command = new SqlCommand(strSql, con);
                command.ExecuteNonQuery();
                string strDetails = "编号: " + strID + "做菜完成";
                RMS.Logger.Add(strCookID, strDetails, RMS.Logger.OTHER);
            }
            catch (Exception e)
            {
                string strDetails = "提交编号: " + strID + "做菜完成失败！详细日志：" + e.Message;
                RMS.Logger.Add(strCookID, strDetails, RMS.Logger.ERROR);
                throw e;
            }
            finally
            {
                if (con != null)
                {
                    con.Close();
                    con.Dispose();
                    con = null;
                }
            }
            return true;
        }
    }
}
